#pragma once
#include <iostream>
#include <vector>
#include <unistd.h>
#include <functional>
#include <ctime>
typedef std::function<void()> task_t; // 包装器

void Download()
{
    std::cout << "这是一个下载任务" << " 执行者是 " << getpid() << std::endl;
}

void PrintLog()
{
    std::cout << "这是一个打印日志任务" << " 执行者是 " << getpid() << std::endl;
}

void PushVideoStream()
{
    std::cout << "这是一个推送视频流任务" << " 执行者是 " << getpid() << std::endl;
}

class Init
{
public:
    Init()
    {
        tasks.push_back(Download);
        tasks.push_back(PrintLog);
        tasks.push_back(PushVideoStream);

        srand(time(nullptr) ^ getpid()); // 生成随机数种子
    }

    // 检查任务码是否合格
    bool CheckSafe(int code)
    {
        if (code >= 0 && code < tasks.size())
            return true;
        else
            return false;
    }

    // 依照任务码执行相应的任务
    void Runtask(int code)
    {
        return tasks[code]();
    }

    // 选择任务码，随机生成
    int SelectTask()
    {
        return rand() % tasks.size();
    }

    // 指向相应的任务，返回对应的字符串
    std::string ToDesc(int code)
    {
        switch (code)
        {
        case g_download_code:
            return "Download";
        case g_printlog_code:
            return "PrintLog";
        case g_push_videostream_code:
            return "PushVideoStream";
        default:
            return "Unknow";
        }
    }

public:
    // 任务码
    static const int g_download_code = 0;
    static const int g_printlog_code = 1;
    static const int g_push_videostream_code = 2;
    std::vector<task_t> tasks;
};

Init init;